package controllers

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/globalsign/mgo/bson"

	"cmt1-admin/lib/request"
	"cmt1-admin/models"
)

type UnionQuery struct {
	BaseRequest
	Name string `form:"name"`
}
type MemberQuery struct {
	BaseRequest
	UnionId string `form:"unionId"`
}

func UnionInfo(c *gin.Context) {
	id := c.Param("unionId")
	res, err := request.Get("union/info/" + id)
	if err != nil {
		handleErr(c, 401, err.Error())
		return
	}
	c.Writer.Header().Set("content-type", "application/json")
	c.String(http.StatusOK, string(res))
}

//通过工会uid，获取工会信息
func GetUnionInfoByUid(c *gin.Context) {
	uid := c.Param("uid")
	res, err := request.Get("union/info/" + uid)
	if err != nil {
		handleErr(c, 401, err.Error())
		return
	}
	c.Writer.Header().Set("content-type", "application/json")
	c.String(http.StatusOK, string(res))
}

func UnionInfoByUid(c *gin.Context) {
	uid := c.Param("uid")
	union := new(models.Union)
	err := GameDB.FindOne("union", bson.M{"_id": bson.ObjectIdHex(uid)}, nil, &union)
	if err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	handleOk(c, map[string]interface{}{"union": union})
}

//通过工会uid，获取工会成员
func GetUnionMembersByUid(c *gin.Context) {
	memberQuery := &MemberQuery{}
	if err := c.ShouldBindQuery(&memberQuery); err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	if memberQuery.Page > 0 {
		memberQuery.Page -= 1
	}

	res, err := request.Get(fmt.Sprintf("union/members?unionId=%s&page=%d&pageNum=%d", memberQuery.UnionId, memberQuery.Page, memberQuery.PageNum))
	if err != nil {
		handleErr(c, 401, err.Error())
		return
	}
	c.Writer.Header().Set("content-type", "application/json")
	c.String(http.StatusOK, string(res))
}

func UnionMembersByUid(c *gin.Context) {
	memberQuery := &MemberQuery{}
	if err := c.ShouldBindQuery(&memberQuery); err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	if memberQuery.Page > 0 {
		memberQuery.Page -= 1
	}

	unionMembers := new(models.UnionMembers)
	err := GameDB.FindOne("unionMembers", bson.M{"_id": bson.ObjectIdHex(memberQuery.UnionId)}, nil, &unionMembers)
	if err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	//将映射的数据加入切片中，方便生成json
	jUnionMembers := new(models.JUnionMembers)
	jUnionMembers.UnionId = unionMembers.UnionId
	for _, val := range unionMembers.Members {
		jUnionMembers.Members = append(jUnionMembers.Members, val)
	}
	handleOk(c, map[string]interface{}{"unionMembers": jUnionMembers})
}

func GetUnionList(c *gin.Context) {
	//转发协议到游戏服务器
	// res, err := request.Get(fmt.Sprintf("union/list?page=%d&pageNum=%d", unionQuery.Page, unionQuery.PageNum))
	res, err := request.Get("union/list?" + c.Request.URL.RawQuery)
	if err != nil {
		handleErr(c, 401, err.Error())
		return
	}
	c.Writer.Header().Set("content-type", "application/json")
	c.String(http.StatusOK, string(res))
}

func UnionList(c *gin.Context) {
	unionQuery := &UnionQuery{}
	if err := c.ShouldBindQuery(&unionQuery); err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	if unionQuery.Page > 0 {
		unionQuery.Page -= 1
	}
	mquery := make(map[string]interface{}, 0)
	count, err := GameDB.Count("union", mquery)
	if err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	unions := make([]*models.Union, 0)
	err = GameDB.FindPage("union", unionQuery.Page, unionQuery.PageNum, mquery, nil, &unions)
	if err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	handleOk(c, map[string]interface{}{"unions": unions, "count": count})
}
